package com.akuhome.resource.service.entity;

import com.akuhome.resource.DO.need.Need;
import com.akuhome.resource.dao.ProjectDao;
import com.akuhome.resource.dto.Form;
import com.akuhome.resource.dto.NeedProject;
import com.akuhome.resource.dto.check.AccProject;
import com.akuhome.resource.dto.check.ClassroomProject;
import com.akuhome.resource.entity.Project;
import com.akuhome.resource.enums.ProjectStatus;
import com.akuhome.resource.service.ApproveService;
import com.akuhome.resource.service.QuartzService;
import com.akuhome.resource.service.need.AccNeedService;
import com.akuhome.resource.service.need.ClassroomNeedService;
import com.akuhome.resource.service.need.FoodNeedService;
import com.akuhome.resource.service.need.NeedService;
import com.akuhome.resource.util.UidBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ProjectService {

    private final Logger logger = LoggerFactory.getLogger(ProjectService.class);

    @Autowired
    ProjectDao projectDao;
    @Autowired
    AccNeedService accNeedService;
    @Autowired
    ClassroomNeedService classroomNeedService;
    @Autowired
    FoodNeedService foodNeedService;
    @Autowired
    ApproveService approveService;
    @Autowired
    private QuartzService quartzService;
    @Autowired
    private UserService userService;

    public List<Project> findAll(){
        return projectDao.findAll();
    }

    public List<Project> findAllByUserId(Integer userId){
        return projectDao.findAllByUserId(userId);
    }

    public List<Project> findAllProjectInMonth(Date month){
        return projectDao.findAllProjectInMonth(month);
    }

    public Project findById(Integer id){return projectDao.findById(id);}

    public Project findByUId(String uid){return projectDao.findByUid(uid);}

    @Transactional(rollbackFor = Exception.class)
    public void newProject(Form form){
        //生成Uid
        String uid = UidBuilder.build();

        Project project = form.getProject();
        project.setUid(uid);
        project.setUser(userService.getCurrentUser());

        form.getAccNeed().setProjectUid(uid);
        form.getClassroomNeed().setProjectUid(uid);
        form.getFoodNeed().setProjectUid(uid);

        //step.1 插入基本数据
        insertProject(project);
        //step.2 插入需求详情
        insertNeed(form);
        //step.3 插入4个审批节点
        approveService.initApproveNode(uid);
    }

    public void confirmProject(Integer id){
        Project project = projectDao.findById(id);
        if(project.getProjectStatus() == ProjectStatus.WAIT_CONFIRM){
            projectDao.confirmProject(id,ProjectStatus.CONFIRMED);
            quartzService.addUpdateProjectStatusJob(project);
            quartzService.addAddCheckJob(project);
        }
    }

    public void insertProject(Project project){
        projectDao.insert(project);
    }

    public List<NeedProject> findAllProjectHaveAccNeed(){
        return projectDao.findAllProjectHaveAccNeed();
    }

    public List<NeedProject>findAllProjectHaveFoodNeed(){
        return projectDao.findAllProjectHaveFoodNeed();
    }

    public List<NeedProject> findAllProjectHaveFoodNeedAll() {
        return projectDao.findAllProjectHaveFoodNeedAll();
    }

    public List<NeedProject> findAllProjectHaveClassroomNeedSession(){
        return projectDao.findAllProjectHaveClassroomNeedAndSession();
    }

    public List<NeedProject> findAllProjectHaveClassroomNeed(){
        return projectDao.findAllProjectHaveClassroomNeedNotSession();
    }

    public List<AccProject> findAllProjectHaveAccCheck(){
        return projectDao.findAllProjectHaveAccCheck();
    }

    public List<Project> findAllProjectHaveFoodCheck(){
        return projectDao.findAllProjectHaveFoodCheck();
    }

    public List<ClassroomProject> findAllProjectHaveClassroomCheck(){
        return projectDao.findAllProjectHaveClassroomCheck();
    }

    public void updateProjectStatus(String uid, ProjectStatus projectStatus){
        projectDao.updateProjectStatus(uid,projectStatus);
    }

    private void insertNeed(Form form){
        List<Map.Entry<NeedService, Need>> serviceAndNeed = new ArrayList<>();
        serviceAndNeed.add(new AbstractMap.SimpleEntry<NeedService,Need>(accNeedService,form.getAccNeed()));
        serviceAndNeed.add(new AbstractMap.SimpleEntry<NeedService,Need>(foodNeedService,form.getFoodNeed()));
        serviceAndNeed.add(new AbstractMap.SimpleEntry<NeedService,Need>(classroomNeedService,form.getClassroomNeed()));

        for(Map.Entry<NeedService,Need> entry : serviceAndNeed){
            insertNeed(entry.getKey(),entry.getValue());
        }
    }

    private void insertNeed(NeedService needService, Need need){
        needService.insertNeed(need);
        Map<String,Object> insertMap = new HashMap<>();
        insertMap.put("id",need.getId());
        insertMap.put("detailList",need.getDetailList());
        if(need.getDetailList().size() > 0)
            needService.insertNeedDetails(insertMap);
    }
}
